package com.nl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.nl.deal.CollectData;
import com.nl.deal.DataCache;
import com.nl.deal.DealDataToSend;
import com.nl.deal.Monitor;
import com.nl.deal.SynchDictData;
import com.nl.deal.TaskMonitor;
import com.nl.deal.ThresholdUpdate;
import com.nl.util.ConfigUtils;
import com.nl.util.ConnectionManager;


public class DealDataToSendMain {//com.nl.DealDataToSendMain

	private static final Logger log = LoggerFactory.getLogger(DealDataToSendMain.class);
	public static String overTime;
	public static String sendCount;
	static{
		overTime = ConfigUtils.getInstance().getValue("overTime");
		sendCount = ConfigUtils.getInstance().getValue("sendCount");
	}
	
	private static List<Thread> threadList = new ArrayList<Thread>();
	public static boolean dictInitSuccessful = false;
	//DealDataToSendMain.thresholdUpdateSuccess
	public static boolean thresholdUpdateSuccess = false;
	public static void main(String[] args) {
		String redisCfg = ConfigUtils.getInstance().getValue("redis");
		String bootstrap = ConfigUtils.getInstance().getValue("bootstrap");
		String groupId = ConfigUtils.getInstance().getValue("groupId");
		String topic = ConfigUtils.getInstance().getValue("topic");
		String master =  ConfigUtils.getInstance().getValue("master");
		String tableName =  ConfigUtils.getInstance().getValue("tableName");
		
//		ConnectionManager.getInstance();
		SynchDictData SynchDictData = new SynchDictData(redisCfg);
		Thread synchThread = new Thread(SynchDictData);
		synchThread.start();
		threadList.add(synchThread);
		while(true) {
			log.info(dictInitSuccessful+"_info_"+DataCache.blackUser.size()+","+DataCache.msgContens.size()+","
					+DataCache.smsTemplateAttrMap.size()+","+DataCache.stationMgrINfoRelationMap.size()+","
					+DataCache.stationMgrRelationMap.size()+","+DataCache.thresholdMap.size());
			if (dictInitSuccessful) {
				if (
						 !DataCache.msgContens.isEmpty() && !DataCache.smsTemplateAttrMap.isEmpty() 
						&& !DataCache.stationMgrINfoRelationMap.isEmpty() && !DataCache.stationMgrRelationMap.isEmpty()
						&& !DataCache.thresholdMap.isEmpty()) {
					log.info("all dict data synch successful!");
					break;
				}
			}
			
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		log.info("dict data synch successful,start deal data!");
		CollectData collectData = new CollectData(bootstrap,topic,groupId);
		Thread collectThread = new Thread(collectData);
		collectThread.start();
		threadList.add(collectThread);
		int dealThread = Integer.valueOf(ConfigUtils.getInstance().getValue("dealThread"));
		for (int i = 0;i < dealThread;i++) {
			DealDataToSend dealDataToSend = new DealDataToSend(redisCfg,ConfigUtils.getInstance().getValue("sender"),master+i,tableName);
			Thread thread = new Thread(dealDataToSend);
			thread.start();
			threadList.add(thread);
		}
		ThresholdUpdate thresholdUpdate = new ThresholdUpdate(redisCfg);
		Thread thresholdThread = new Thread(thresholdUpdate);
		thresholdThread.start();
		threadList.add(thresholdThread);
		TaskMonitor monitor = TaskMonitor.getInstance();
		monitor.startTaskMonitor(redisCfg);
		log.info("monitor start");
		Monitor monitor1 = Monitor.getInstance();
		monitor1.setThreadList(threadList);
		monitor1.startMonitor();
	}

}
